/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.FumeHood;
import me.zhengjie.modules.biz.service.FumeHoodService;
import me.zhengjie.modules.biz.service.dto.FumeHoodQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-16
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "fume-hood管理")
@RequestMapping("/api/fumeHood")
public class FumeHoodController {

    private final FumeHoodService fumeHoodService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('fumeHood:list')")
    public void exportFumeHood(HttpServletResponse response, FumeHoodQueryCriteria criteria) throws IOException {
        fumeHoodService.download(fumeHoodService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询fume-hood")
    @ApiOperation("查询fume-hood")
//    @PreAuthorize("@el.check('fumeHood:list')")
    public ResponseEntity<Object> queryFumeHood(FumeHoodQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(fumeHoodService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增fume-hood")
    @ApiOperation("新增fume-hood")
//    @PreAuthorize("@el.check('fumeHood:add')")
    public ResponseEntity<Object> createFumeHood(@Validated @RequestBody FumeHood resources){
        return new ResponseEntity<>(fumeHoodService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改fume-hood")
    @ApiOperation("修改fume-hood")
//    @PreAuthorize("@el.check('fumeHood:edit')")
    public ResponseEntity<Object> updateFumeHood(@Validated @RequestBody FumeHood resources){
        fumeHoodService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除fume-hood")
    @ApiOperation("删除fume-hood")
//    @PreAuthorize("@el.check('fumeHood:del')")
    public ResponseEntity<Object> deleteFumeHood(@RequestBody Integer[] ids) {
        fumeHoodService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/queryDataFromPLC")
//    @Log("读取PLC数据")
    @ApiOperation("读取PLC数据")
    public ResponseEntity<Object> queryDataFromPLC(){
        fumeHoodService.queryDataFromPLC();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/openFumeHood")
    @Log("启动通风柜")
    @ApiOperation("启动通风柜")
    public ResponseEntity<Object> openFumeHood(@RequestParam int id){
        fumeHoodService.openFumeHood(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/closeFumeHood")
    @Log("关闭通风柜")
    @ApiOperation("关闭通风柜")

    public ResponseEntity<Object> closeFumeHood(@RequestParam int id){
        fumeHoodService.closeFumeHood(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
